package data;

import mat.xls.Mat;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DataHelper {
    public static Map<String, String> readProps(Props propsPrefix, String fileName) throws IOException {
        BufferedReader reader;
        String line;
        try {
            reader = new BufferedReader(new FileReader(fileName));
        } catch (FileNotFoundException e) {
            return null;
        }
        Map<String, String> props = new HashMap<String, String>();
        boolean moved = false;
        String lastKey = "";
        while ((line = reader.readLine()) != null) {
            if (line.startsWith("#")) continue;
            if (line.contains("﻿")) {
                line = line.replace("﻿", "");
            }
            if (moved) {
                String value = props.get(lastKey);
                String newValue = value.replace("\\", line);
                props.put(lastKey, newValue);
                if (props.get("item.name")!= null && props.get("item.name").contains("силенный") && lastKey.equals("item.desc")) {
                    System.out.println();
                }
                if (!newValue.trim().endsWith("\\")) {
                    moved = false;
                }
                continue;
            }
            String[] keyvalue = line.split("=");
            String key = keyvalue[0];
            if (propsPrefix != null) {
                if (!key.startsWith(propsPrefix.name)) {
                    continue;
                } else {
                    String suffix = key.substring(propsPrefix.name.length() + 1, key.length());
                    if (suffix.contains(".")) {
                        continue;
                    }
                }
            }
            if (keyvalue.length == 1) {
                props.put(key, "");
            } else if (keyvalue.length == 2) {
                String value = keyvalue[1];
                props.put(key, value);
                if (value.endsWith("\\")) {
                    moved = true;
                    lastKey = key;
                }
            } else {
                String value = "";
                int count = 0;
                for (String field : keyvalue) {
                    if (count != 0) {
                        value += field + "=";
                    }
                    count++;
                }
                value = value.substring(0, value.length() - 1); // remove last equals sign
                props.put(key, value);
                if (value.endsWith("\\")) {
                    moved = true;
                    lastKey = key;
                }
            }
        }
        reader.close();
        return props;
    }

    public static Map<String, String> readProps(String fileName) throws IOException {
        return readProps(null, fileName);
//        BufferedReader reader;
//        String line;
//        reader = new BufferedReader(new FileReader(fileName));
//        Map<String, String> accums = new HashMap<String, String>();
//        boolean moved = false;
//        String lastKey = "";
//        while ((line = reader.readLine()) != null) {
//            if (line.startsWith("#")) continue;
//            if (line.contains("﻿")) {
//                line = line.replace("﻿", "");
//            }
//            if (moved) {
//                String value = accums.get(lastKey);
//                String newValue = value.replace("\\", line);
//                accums.put(lastKey, newValue);
//                if (!newValue.endsWith("\\")) {
//                    moved = false;
//                }
//                continue;
//            }
//            String[] keyvalue = line.split("=");
//            String key = keyvalue[0];
//            if (keyvalue.length == 1) {
//                accums.put(key, "");
//            } else if (keyvalue.length == 1) {
//                String value = keyvalue[1];
//                accums.put(key, value);
//                if (value.endsWith("\\")) {
//                    moved = true;
//                    lastKey = key;
//                }
//            } else {
//                String value = "";
//                int count = 0;
//                for (String field : keyvalue) {
//                    if (count != 0) {
//                        value += field;
//                    }
//                    count++;
//                }
//                accums.put(key, value);
//                if (value.endsWith("\\")) {
//                    moved = true;
//                    lastKey = key;
//                }
//            }
//        }
//        return accums;
    }

    public static Map<String, List<Mat>> splitByCategory(List<Mat> mats) {
        Map<String, List<Mat>> matsByCategory = new HashMap<String, List<Mat>>();
        for (Mat mat : mats) {
            String cat = mat.category;
            List<Mat> matsList = matsByCategory.get(cat);
            if (matsList == null) {
                matsList = new ArrayList<Mat>();
                matsByCategory.put(cat, matsList);
            }
            matsList.add(mat);
        }
        return matsByCategory;
    }

    public static String codeInURL(String value) {
        value = value.trim().replaceAll("\\s", "_").toLowerCase();
        value = value.replaceAll("\\(", "").replaceAll("\\)", "");
        value = value.replaceAll("@", "at");
        value = value.replaceAll("\\+", "plus");
        value = value.replaceAll("\\&", "and");
        value = value.replaceAll("\\.", "dot");
        value = value.replaceAll("\"", "");
        value = value.replaceAll("'", "");
        value = value.replaceAll("мтс", "mts");
        value = value.replaceAll("маячок", "majachok");
        value = value.replaceAll("жучок", "zhuchok");
        value = value.replaceAll("крош", "krosh");
        return value;
    }

    public enum Props {
        CATEGORY("category"),
        CATEGORY_BRAND("category.brand"),
        ITEM("item"),
        ITEM_MODEL("item.model"),
        COMMON("common");

        public String name;

        Props(String name) {
            this.name = name;
        }
    }
}
